<?php
require_once 'models/TMSCartModel.inc';

/**
 * @author dkrasilnikov@gmail.com
 * @property TResponse $Response
 * @property TMSCartModel $CartModel
 */

class TCart extends TConfigurable {
/**
 * @var TMSCartModel
 */	
	protected $_ioc_cart_model_;
/**
 * @var TCatalogProductModel
 */	
	protected $_ioc_product_model_;
	
	private $_total_sum_ = null;
	
	private $_total_count_ = null;
	
	private $_cart_;
	
	private $_positions_;
	
	protected function isAuthorized(){
		$u = $this->Application()->CurrentService()->Acl->CurrentUser();
		return ($u instanceof TMSUser) && ($u->Roles & TMSRole::CLIENT);
	}
	
	protected function checkTotals(){
		if (is_null($this->_total_sum_)){
			$cart = $this->_get_cart();
			$this->_total_sum_ = 0;
			$this->_total_count_ = 0;
			foreach ($this->_positions_ as $p){
				$this->_total_count_ += $p->Quantaty;
				$this->_total_sum_ += $p->Quantaty*$p->Price;
			}
		}
	}
	
	public function TotalSum(){
		$this->checkTotals();
		return $this->_total_sum_;
	}
	
	public function TotalCount(){
		$this->checkTotals();
		return $this->_total_count_;		
	}
		
	private function _get_cart(){
		if (is_array($this->_cart_))
			return $this->_cart_;
		$this->_cart_ = array();
		$this->_positions_ = array();
		if ($this->isAuthorized()){
			$res = $this->CartModel->GetList();
			foreach ($res as $pos){
				$this->_cart_[$pos->Id] = array();
				foreach ($pos->Properties() as $p) 
					$this->_cart_[$pos->Id][$p->Name()] = $p->Value();
				$this->_positions_[] = $pos;
			}
			return $this->_cart_;
		}
		$this->_cart_ = $this->Application()->Session()->Get('cart');
		if (!is_array($this->_cart_)) $this->_cart_ = array();
		foreach ($this->_cart_ as $id => $data){
			$obj = $this->CartModel->GetDummyItem();
			if ($obj->Product = $this->ProductModel->GetItem($data['Product'])){
				foreach ($data as $nm => $v)
					$obj->$nm = $v;
				$this->_positions_[$obj->ItemId()] = $obj;
			}
		}
		return $this->_cart_;
	}
	
	public function Positions(){
		$this->_get_cart();
		return new TArrayIterator($this->_positions_);	
	}
	
	public function AddPosition($product, array $options = array(),$quantaty = 1){
		$d = $this->CartModel->GetDummyItem();
		$d->Product = $product;
		foreach ($options as $nm => $v)
			$d->Base->SetOption($nm,$v);		
		
		if ($this->isAuthorized()){			
			$item = $this->CartModel->GetItem($d->ItemId());
			if ($item)
				return $this->SetQuantaty($d->ItemId(), $quantaty + $item->Quantaty);
			return $this->CartModel->CreateItem(array('Product'=>$product,'Quantaty'=>$quantaty) + $options);
		}
		$this->_get_cart();
		if (isset($this->_cart_[$d->ItemId()]))
			$this->SetQuantaty($d->ItemId(), $this->_cart_[$d->ItemId()]['Quantaty'] + $quantaty);
		else {	
			$this->_cart_[$d->ItemId()] = array("Quantaty"=>$quantaty);
			foreach ($d->Properties() as $p)
				$this->_cart_[$d->ItemId()][$p->Name()] = $p->Value();
			$d->Quantaty = $quantaty;
			$this->_positions_[$d->ItemId()] = d;
			$this->Application()->Session()->Set('cart', $this->_cart_);
		}
		$this->_total_sum_ = null;
	}
	
	public function DeletePosition($id){
		if ($this->isAuthorized())
			return $this->CartModel->DeleteItem($id);
		$this->_get_cart();	
		unset($this->_cart_[$id]);
		$this->Application()->Session()->Set('cart', $this->_cart_);
	}
	
	public function MakeEmpty(){
		if ($this->isAuthorized()){
			$this->CartModel->MakeEmpty();
		} else 	
			$this->Application()->Session()->Set('cart',null);
			
		$this->_cart_ = array();
		$this->_positions_ = array();
	}
	
	public function EditPosition($id,array $data = array()){
		if ($this->isAuthorized())
			return $this->CartModel->EditItem($id, $data);
		else {
			$this->_get_cart();
			$pos = clone $this->_positions_[$id];
			foreach ($data as $nm=>$v){
				if ($nm == 'Quantaty')
					$pos->$nm = $data[$nm];
				else
					$pos->Base->SetOption($nm,$data[$nm]);
			}
			
			unset($this->_positions_[$id]);
			unset($this->_cart_[$id]);
			
			$this->_positions_[$pos->ItemId()] = $pos;
			foreach ($data as $nm=>$v)
				$this->_cart_[$pos->ItemId()][$nm] = $v;
			$this->Application()->Session()->Set('cart',$this->_cart_);
		}
	}
	
	public function SetQuantaty($id, $quantaty){
		$this->EditPosition($id,array('Quantaty'=>$quantaty));
	}
}